package com.redstars.generate.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.redstars.generate.config.GenDataSource;
import com.redstars.generate.entity.SysDatasource;
import com.redstars.generate.entity.SysTable;
import com.redstars.generate.page.PageResult;
import com.redstars.generate.result.Result;
import com.redstars.generate.utils.GenUtils;
import com.redstars.generate.vo.SysDatasourceVo;
import com.redstars.generate.vo.SysTableVo;
import lombok.AllArgsConstructor;
import com.redstars.generate.service.SysDatasourceService;
import com.redstars.generate.query.SysDatasourceQuery;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
* 数据源管理
*
* @author zhouhx 
* @since 1.0.0 2022-10-05
*/
@RestController
@RequestMapping("gen/datasource")
@AllArgsConstructor
public class SysDatasourceController {
    private final SysDatasourceService sysDatasourceService;

    /**
     *
     * @author zhuohx
     * @description 分页
     * @parms  [query]
     * @return com.redstars.generate.result.Result<com.redstars.generate.page.PageResult<com.redstars.generate.vo.SysDatasourceVo>>
     * @throws
     * @date 2022/10/5 15:49
     */
    @GetMapping("page")
    public Result<PageResult<SysDatasourceVo>> page(@Valid SysDatasourceQuery query){
        LambdaQueryWrapper<SysDatasource> queryWrapper = Wrappers.lambdaQuery();
        if(StrUtil.isNotBlank(query.getDbType())){
            queryWrapper.eq(SysDatasource::getDbType,query.getDbType());
        }
        if(StrUtil.isNotBlank(query.getConnName())){
            queryWrapper.eq(SysDatasource::getConnName,query.getConnName());
        }
        PageResult<SysDatasourceVo> page = sysDatasourceService.page(query);

        return Result.ok(page);
    }

    /**
     *
     * @author zhuohx
     * @description 详情
     * @parms  [query]
     * @return com.redstars.generate.result.Result<com.redstars.generate.vo.SysDatasourceVo>
     * @throws
     * @date 2022/10/5 15:49
     */
    @GetMapping("{id}")
    public Result<SysDatasourceVo> detail(@PathVariable("id") Long id){
        SysDatasourceVo detail = sysDatasourceService.detail(id);

        return Result.ok(detail);
    }

    /**
     *
     * @author zhuohx
     * @description 保存
     * @parms  [vo]
     * @return com.redstars.generate.result.Result<java.lang.String>
     * @throws
     * @date 2022/10/5 15:49
     */
    @PostMapping
    public Result<String> save(@RequestBody SysDatasourceVo vo){
        sysDatasourceService.save(vo);

        return Result.ok();
    }

    /**
     *
     * @author zhuohx
     * @description 修改
     * @parms  [vo]
     * @return com.redstars.generate.result.Result<java.lang.String>
     * @throws
     * @date 2022/10/5 15:49
     */
    @PutMapping
    public Result<String> update(@RequestBody @Valid SysDatasourceVo vo){
        sysDatasourceService.update(vo);

        return Result.ok();
    }

    /**
     *
     * @author zhuohx
     * @description 删除
     * @parms  [idList]
     * @return com.redstars.generate.result.Result<java.lang.String>
     * @throws
     * @date 2022/10/5 15:50
     */
    @DeleteMapping
    public Result<String> delete(@RequestBody List<Long> idList){
        sysDatasourceService.delete(idList);

        return Result.ok();
    }

    @GetMapping("list")
    public Result<List<SysDatasourceVo>> list(SysDatasourceQuery query) {
        List<SysDatasourceVo> list = sysDatasourceService.list(query);

        return Result.ok(list);
    }

    @GetMapping("test/{id}")
    public Result<String> test(@PathVariable("id") Long id) {
        return sysDatasourceService.test(id);
    }

    /**
     * 根据数据源ID，获取全部数据表
     *
     * @param id 数据源ID
     */
    @GetMapping("table/list/{id}")
    public Result<List<SysTableVo>> tableList(@PathVariable("id") Long id) {
        return sysDatasourceService.tableList(id);
    }

    /**
     * 根据数据源ID和表名，获取数据表
     *
     * @param id 数据源ID
     */
    @GetMapping("table/list/search/{id}")
    public Result<List<SysTableVo>> tableListSearch(@PathVariable("id") Long id,@RequestParam("tableName") String tableName) {
        return sysDatasourceService.tableList(id,tableName);
    }

}